<template>
  <div class="checkout-address">
    <div class="text">
      <div class="none" v-if="!showAddress">您需要先添加收货地址才可提交订单。</div>
      <ul v-if="showAddress">
        <li>
          <span>
            收
            <i />货
            <i />人：
          </span>
          {{ showAddress.receiver }}
        </li>
        <li>
          <span>联系方式：</span>
          {{ showAddress.contact }}
        </li>
        <li>
          <span>收货地址：</span>
          {{ showAddress.fullLocation.replace(/ /g, '') + showAddress.address }}
        </li>
      </ul>
      <a v-if="showAddress" @click="openAddressEdit(showAddress)" href="javascript:;">修改地址</a>
    </div>
    <div class="action">
      <Button class="btn" @click="dialogVisible = true">切换地址</Button>
      <Button class="btn" @click="openAddressEdit()">添加地址</Button>
    </div>
  </div>

  <!-- 切换收货地址对话框 -->
  <Dialog title="切换收货地址" v-model:visible="dialogVisible">
    <div
      class="text item"
      v-for="item in addressList"
      :key="item.id"
      :class="{ active: selectedAddress && selectedAddress.id === item.id }"
      @click="selectedAddress = item"
    >
      <ul>
        <li>
          <span>
            收
            <i />货
            <i />人：
          </span>
          {{ item.receiver }}
        </li>
        <li>
          <span>联系方式：</span>
          {{ item.contact }}
        </li>
        <li>
          <span>收货地址：</span>
          {{ item.fullLocation.replace(/ /g, '') + item.address }}
        </li>
      </ul>
    </div>
    <template #footer>
      <Button @click="dialogVisible = false" style="margin-right: 30px;" type="gray">取消</Button>
      <Button @click="confirmAddress" type="primary">确认</Button>
    </template>
  </Dialog>

  <!-- 添加或修改对话框 -->
  <addressEditVue @on-success="successHandler" ref="addressEditCom" />
</template>
<script>
import { ref, watch } from "vue"
import addressEditVue from "./address-edit.vue"
export default {
  name: 'CheckoutAddress',
  props: {
    addressList: {
      type: Array,
      default: () => []
    }
  },
  emits: ['change'],
  components: { addressEditVue },
  setup(props, { emit }) {
    //默认地址是否展示
    const showAddress = ref(null)
    if (props.addressList.length) {
      // 获取用户地址列表中最新地址
      const defaultAddress = props.addressList.find(item => item.isDefault === 1)
      if (defaultAddress) {
        showAddress.value = defaultAddress
      } else {
        // eslint-disable-next-line vue/no-setup-props-destructure
        showAddress.value = props.addressList[0]
      }
    }
    // 初始化默认发送地址id给父组件
    emit('change', showAddress.value && showAddress.value.id)

    // 使用子组件的方法控制显示 
    const addressEditCom = ref(null)
    const openAddressEdit = (address) => {
      addressEditCom.value.open(address)
    }

    // 保存添加/修改的位置信息
    const successHandler = (formData) => {
      const editAddress = props.addressList.find(item => item.id === formData.id)
      if (editAddress) {
        // 修改地址-引用对象直接修改
        for (const key in editAddress) {
          editAddress[key] = formData[key]
        }
      } else {
        // 添加
        const json = JSON.stringify(formData) // 需要克隆下，不然使用的是对象的引用
        // eslint-disable-next-line vue/no-mutating-props
        props.addressList.unshift(JSON.parse(json))
      }
    }


    // 控制选择地址对话框
    const dialogVisible = ref(false)
    // 选中的地址
    const selectedAddress = ref(null)

    // 确认切换地址
    const confirmAddress = () => {
      dialogVisible.value = false
      showAddress.value = selectedAddress.value
      // 默认通知一个地址ID给父组件
      emit('change', showAddress.value && showAddress.value.id)
    }
    // 关闭切换地址确认框
    watch(dialogVisible, (newVal) => {
      if (!newVal) {
        selectedAddress.value = null
      }
    })

    return {
      showAddress,
      dialogVisible,
      addressEditCom,
      openAddressEdit,
      successHandler,
      selectedAddress,
      confirmAddress,
    }
  }
}
</script>

<style scoped lang="less">
.dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,
      &:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor, 50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>